#include <buola/gui.h>

#include <buola/algorithm/container.h>
#include <buola/image/cimage.h>
#include <buola/gui/cemblemicon.h>
#include <buola/gui/cicon.h>

namespace buola { namespace gui {

std::vector<CEmblemIcon*> CEmblemIcon::mEmblemIcons;

CEmblemIcon::CEmblemIcon()
{
    mEmblemIcons.push_back(this);
    mIcon=nullptr;
}

CEmblemIcon::~CEmblemIcon()
{
    erase_first_equal(mEmblemIcons,this);
}

bool CEmblemIcon::Init(PIcon pIcon,const std::vector<std::string> &pList)
{
    mIcon=pIcon;
    mList=pList;

    for(int i=0;i<pList.size();i++)
    {
        mEmblems.push_back(CIcon::Get("emblem-"+pList[i]));
    }

    return true;
}

bool CEmblemIcon::Draw(img::CGC *pGC,const CPoint_d &pPos,int pSize,EStyle pStyle)
{
    mIcon->Draw(pGC,pPos,pSize,pStyle);

    int lX[4]={ pSize/2,    pSize/2,    0,          0   };
    int lY[4]={ 0,          pSize/2,    pSize/2,    0   };

    for(int i=0;i<mEmblems.size();i++)
    {
        mEmblems[i]->Draw(pGC,pPos+CPoint_d(lX[i%4],lY[i%4]),pSize/2,pStyle);
    }

    return true;
}

img::CPicture CEmblemIcon::GetPixmap(int pSize,EStyle pStyle)
{
    return mIcon->GetPixmap(pSize,pStyle);
}

PIcon CEmblemIcon::Get(PIcon pIcon,const std::vector<std::string> &pList)
{
    for(int i=0;i<mEmblemIcons.size();i++)
    {
        if(pIcon==mEmblemIcons[i]->mIcon&&pList==mEmblemIcons[i]->mList)
        {
            return mEmblemIcons[i];
        }
    }

    CEmblemIcon *lIcon=new CEmblemIcon;

    if(!lIcon->Init(pIcon,pList))
    {
        delete lIcon;
        return nullptr;
    }

    return lIcon;
}

/*namespace gui*/ } /*namespace buola*/ }
